package com.usooft.metaTalent.core.platform.service.base;

import com.usooft.framework.core.query.Limit;
import com.usooft.framework.core.query.Page;
import com.usooft.metaTalent.core.platform.entity.ArticleTag;
import com.usooft.metaTalent.core.platform.mapper.ArticleTagMapper;
import com.usooft.metaTalent.core.platform.query.ArticleTagQuery;
import com.usooft.metaTalent.core.platform.service.ArticleTagService;
import com.usooft.metaTalent.core.platform.wrapper.ArticleTagWrapper;
import java.lang.Long;
import java.lang.Override;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.lang.NonNull;

public abstract class BaseArticleTagService implements ArticleTagService {
    @Autowired
    private ArticleTagMapper articleTagMapper;

    @Override
    public ArticleTag get(@NonNull Long id) {
        return find(id).orElseThrow();
    }

    @Override
    public Optional<ArticleTag> find(@NonNull Long id) {
        return Optional.ofNullable(articleTagMapper.get(id));
    }

    @Override
    public List<ArticleTagWrapper> list(ArticleTagQuery query) {
        return articleTagMapper.list(query);
    }

    @Override
    public Page<ArticleTagWrapper> page(ArticleTagQuery query) {
        int rows = articleTagMapper.count(query.getFilter());
        return new Page<>(rows, rows == 0 ? new ArrayList<>() : articleTagMapper.list(query));
    }

    protected Optional<ArticleTag> findOne(ArticleTagQuery.Filter filter,
            ArticleTagQuery.Sorts sorts) {
        List<ArticleTagWrapper> wrappers = list(ArticleTagQuery.builder()
                                .filter(filter)
                                .sorts(sorts)
                                .limit(Limit.rowCount(1))
                                .build());
        return wrappers.stream().findFirst().map(Function.identity());
    }

    protected Optional<ArticleTag> findOne(ArticleTagQuery.Filter filter) {
        return findOne(filter, ArticleTagQuery.Sorts.builder().build());
    }

    @Override
    public int count(ArticleTagQuery.Filter filter) {
        return articleTagMapper.count(filter);
    }

    protected ArticleTag internalAdd(Long articleId, Long tagId) {
        ArticleTag articleTag = new ArticleTag();
        articleTag.setArticleId(articleId);
        articleTag.setTagId(tagId);

        articleTag.setCreatedDate(LocalDateTime.now());
        articleTag.setVersion(0);
        articleTag.setIsDelete(false);
        articleTagMapper.add(articleTag);
        return articleTag;
    }

    protected void internalUpdate(@NonNull ArticleTag articleTag) {
        articleTag.setLastModifiedDate(LocalDateTime.now());

        int rows = articleTagMapper.update(articleTag);
        if (rows == 0) {
            throw new OptimisticLockingFailureException("更新失败。可能文章标签 (" + articleTag.getId() + ") 的版本 (" + articleTag.getVersion() + ") 已不是最新");
        }
        articleTag.setVersion(articleTag.getVersion() + 1);
    }
}
